package pub.guoxin.oops.manager.controller;

import com.querydsl.core.types.dsl.BooleanExpression;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pub.guoxin.oops.annotation.InjectUser;
import pub.guoxin.oops.constants.OopsPathConstants;
import pub.guoxin.oops.manager.service.GroupService;
import pub.guoxin.oops.manager.vo.GroupIdNameVo;
import pub.guoxin.oops.manager.vo.GroupPageVo;
import pub.guoxin.oops.manager.vo.GroupVo;
import pub.guoxin.oops.model.Group;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.vo.ResultVo;
import pub.guoxin.oops.vo.ResultVoBuilder;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * Created by guoxin on 17-11-25.
 */
@RestController
@PreAuthorize(value = "hasAnyRole('ADMIN')")
@RequestMapping(OopsPathConstants.GROUP_PATH)
public class GroupController {

    @Autowired
    private ResultVoBuilder builder;
    @Autowired
    private GroupService groupService;

    @ApiOperation(value = "添加", notes = "添加分组(权限：公司管理员)")
    @PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo add(@InjectUser User user, @Valid @RequestBody GroupVo groupAddVo) {
        Group group = groupService.add(user, groupAddVo);
        if (Objects.isNull(group)){
            return builder.buildBadRequestResultInfo("子帐户不存在");
        }
        return builder.buildSuccessResultInfo();
    }

    @ApiOperation(value = "删除分组", notes = "删除分组(权限：公司管理员)")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo del(@InjectUser User user, @PathVariable String id) {
        Group group = groupService.del(id);
        if (Objects.isNull(group)) {
            return builder.buildBadRequestResultInfo("分组不存在");
        }
        return builder.buildSuccessResultInfo();
    }

    @ApiOperation(value = "查询分组", notes = "查询分组(分页查询所有分组，权限：公司管理员)")
    @GetMapping(value = "/page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo<Page<GroupPageVo>> get(@InjectUser User user,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
                                           @RequestParam(required = false, defaultValue = "15") Integer pageSize) {
        Page<GroupPageVo> page = groupService.page(user, pageNumber, pageSize);
        return builder.buildSuccessResultInfo(page);
    }

    @ApiOperation(value = "查询分组集合", notes = "查询分组集合(分页查询所有分组，权限：公司管理员)")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo get(@InjectUser User user) {
        List<GroupIdNameVo> list = groupService.list(user.getCompanyId());
        return builder.buildSuccessResultInfo(list);
    }

    @ApiOperation(value = "修改分组", notes = "删除分组(权限：公司管理员)")
    @PutMapping(value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo del(@InjectUser User user, @Valid @RequestBody GroupVo groupAddVo) {
        Group group = groupService.modify(user, groupAddVo);
        if (Objects.isNull(group)) {
            return builder.buildBadRequestResultInfo("该分组不存在");
        }
        return builder.buildSuccessResultInfo();
    }

}
